package com.hqj.client.controller;

import com.hqj.client.entity.UserRole;
import com.hqj.client.service.UserRoleService;

import com.hqj.common.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 黄啟军
 * @version 1.0
 * @date 2020/5/16/016
 */
@Api(tags = "UserRoleController")
@RestController
@RequestMapping("/userRole")
public class UserRoleController {

    @Resource
    private UserRoleService userRoleService;
    /***
     * UserRole分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "UserRole分页查询",notes = "分页查询UserRole方法详情",tags = {"UserRoleController"})
    @GetMapping(value = "/search/{page}/{size}" )
    public String findPage(@PathVariable  int page,
                           @PathVariable  int size, Model model){
        //调用UserRoleService实现分页查询UserRole
       List<UserRole> pageInfo = userRoleService.findPage(page, size);
       model.addAttribute("findPage",pageInfo);
        return "/page/BlogIndex";
    }
    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "UserRole根据ID删除",notes = "根据ID删除UserRole方法详情",tags = {"UserRoleController"})
    @DeleteMapping(value = "delete/{id}" )
    public String delete(@PathVariable Long id,Model model){
        //调用UserRoleService实现根据主键删除
        int i = userRoleService.delete(id);
        if (i>0){
            model.addAttribute("delete","删除成功");
        }
        return "/page/BlogIndex";
    }

    /***
     * 修改UserRole数据
     * @param userRole
     * @param id
     * @return
     */
    @ApiOperation(value = "UserRole根据ID修改",notes = "根据ID修改UserRole方法详情",tags = {"UserRoleController"})
    @PutMapping(value="update/{id}")
    public String update(@RequestBody @ApiParam(name = "UserRole对象",value = "传入JSON数据",required = false) UserRole userRole,
                         @PathVariable Long id,Model model){
        //设置主键值
        userRole.setUserId(id);
        //调用UserRoleService实现修改UserRole
        int i = userRoleService.update(userRole);
        if (i>0){
            model.addAttribute("update","修改成功");
        }
        return "/page/BlogIndex";
    }

    /***
     * 新增UserRole数据
     * @param userRole
     * @return
     */
    @ApiOperation(value = "UserRole添加",notes = "添加UserRole方法详情",tags = {"UserRoleController"})
    @PostMapping("/insert")
    public String insert(@RequestBody  @ApiParam(name = "UserRole对象",value = "传入JSON数据",required = true) UserRole userRole,Model model){
        //调用UserRoleService实现添加UserRole
        int i = userRoleService.add(userRole);
        if (i>0){
            model.addAttribute("inset","增加成功");
        }
        return "/page/BlogIndex";
    }

    /***
     * 根据ID查询UserRole数据
     * @param id
     * @return
     */
    @ApiOperation(value = "UserRole根据ID查询",notes = "根据ID查询UserRole方法详情",tags = {"UserRoleController"})
    @GetMapping("findById/{id}")
    public String findById(@PathVariable Long id, Model model){
        //调用UserRoleService实现根据主键查询UserRole
        UserRole userRole = userRoleService.findById(id);
        if (userRole!=null){
            model.addAttribute("findById",userRole);
        }

        return "/page/BlogIndex";
    }
    /***
     * 查询UserRole全部数据
     * @return
     */
    @ApiOperation(value = "查询所有UserRole",notes = "查询所UserRole有方法详情",tags = {"UserRoleController"})
    @GetMapping("/findAll")
    public String findAll(Model model){
        //调用UserRoleService实现查询所有UserRole
        List<UserRole> list = userRoleService.findAll();
        if (list.isEmpty()){
            model.addAttribute("findAll","未查询到");
        }else {
            model.addAttribute("findAll",list);
        }
        return "/page/BlogIndex";
    }
}
